package activity.myrecyclerview;

import android.app.Activity;
import android.support.annotation.UiThread;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Button;

import com.Decoration.SpacesItemDecoration;
import com.adapter.RecyclerAdapter;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

import recyclerview.animators.SlideInUpAnimator;

@EActivity(R.layout.activity_recycler)
public class RecyclerActivity extends Activity {
    @ViewById(R.id.recyclerview)
    public RecyclerView recyclerview;
    @ViewById(R.id.add)
    public Button add;
    @ViewById(R.id.del)
    public Button del;
    public RecyclerAdapter recyclerAdapter;
    private List<Integer> imglist = new ArrayList<Integer>();
    private Integer[] img = new Integer[]{R.mipmap.a1, R.mipmap.a2, R.mipmap.a3, R.mipmap.a4};

    @AfterViews
    public void init() {
        load();
    }

    @Click({R.id.add, R.id.del})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.add:
                recyclerAdapter.add(R.mipmap.a5, 1);
                break;
            case R.id.del:
                recyclerAdapter.remove(1);
                break;
        }

    }

    @UiThread
    public void load() {
        for (int i = 0; i < img.length; i++) {
            imglist.add(img[i]);
        }
        //设置layoutManager
        recyclerview.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        recyclerview.setItemAnimator(new SlideInUpAnimator());//new SlideInUpAnimator()

        //设置item之间的间隔
        SpacesItemDecoration decoration = new SpacesItemDecoration(16);
        recyclerview.addItemDecoration(decoration);
        recyclerview.setHasFixedSize(true);

    }

    @Override
    protected void onResume() {
        recyclerAdapter = new RecyclerAdapter(this, imglist);
        recyclerview.setAdapter(recyclerAdapter);
        super.onResume();
    }
}
